import pyautogui
import numpy as np
import cv2
from PIL import Image,ImageDraw,ImageFont,ImageGrab

import time
import PySimpleGUI as sg
import datetime
import io
from PIL import Image

#OCR文字识别部分
#import ddddocr
#ocr = ddddocr.DdddOcr() 

def action():
    pass

#定义界面GUI元素
layout = [[sg.Text('请鼠标划定区域', key='-OUTPUT-')],
          [sg.Button('开始'), sg.Exit()],
          [sg.Image(key="-IMAGE1-")],
          ]    
window = sg.Window('屏幕实时截取', layout,keep_on_top=True)
#点击按钮后，截屏，并全屏显示    

while True:

    #这里定义了刷新频率 单位是毫秒    
    event, values = window.read(timeout=1000)


    
    #window['-OUTPUT-'].update(values['-IN-'])
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    #截取屏幕
    im = pyautogui.screenshot()

    img=np.array(im)
    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    img_copy=img.copy()
    #print ('start here1')
        #img =  cv2.imread('1.png')


    if event=='开始':
        #从全屏图片中选择感兴趣的区域
        cv2.namedWindow("ROI selector", cv2.WND_PROP_FULLSCREEN)
        cv2.setWindowProperty("ROI selector",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

        r = cv2.selectROI(img_copy)

        cv2.destroyAllWindows()
   
    if 'r' not in locals():

        continue

    imCrop = img_copy[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]

    image = Image.fromarray(cv2.cvtColor(imCrop,cv2.COLOR_BGR2RGB))
    bio = io.BytesIO()
    image.save(bio, format="PNG")
    window["-IMAGE1-"].update(data=bio.getvalue())
    action()

window.close()

        
            
        

